package GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.net.MalformedURLException;
import java.util.HashMap;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;

import Client.ChatClientSendThread;
import Mailbox.Mailbox;
import Mailbox.MailboxFTP;

public class GUIchatroom extends JFrame implements MouseListener {
	private static final long serialVersionUID = 27L;

	private JPanel chatWindow; // The chat window
	public JTextArea output; // The window where the messages are displayed
	private JTextArea input; // The window where the user writes his message

	private JScrollPane outputPane; // Scroll for the output window
	private JScrollPane inputPane; // Scroll for the input window

	private JButton sendButton; // Sends a message

	private JButton exitButton; // Exits the program

	private JScrollPane friendsPane; // Scroll for the list of friends
	public JList friendsList; // The list of friends
	private DefaultListModel friendsModel;

	private HashMap<String, GUIprivate> privteChatrooms;

	private String title;

	private ChatClientSendThread client;

	private GUIstartpage sp;

	public GUIchatroom(String username, String title,
			ChatClientSendThread client, GUIstartpage sp) {
		this.sp = sp;
		this.title = title;
		this.client = client;

		privteChatrooms = new HashMap<String, GUIprivate>();

		chatWindow = new JPanel();
		setTitle(username + " | groupchat: " + title);

		output = new JTextArea();

		output.setEditable(false);
		outputPane = new JScrollPane(output);
		outputPane.setPreferredSize(new Dimension(400, 240));
		outputPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

		input = new JTextArea("", 5, 36);
		input.setLineWrap(true);
		input.addKeyListener(keyListener);
		input.requestFocus();
		inputPane = new JScrollPane(input);

		friendsModel = new DefaultListModel();
		friendsList = new JList(friendsModel);

		friendsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		friendsList.setSelectedIndex(0);
		friendsList.addMouseListener(this);
		friendsList.setVisibleRowCount(15);
		friendsList.setPreferredSize(new Dimension(100, 200));
		friendsPane = new JScrollPane(friendsList);

		sendButton = new SendButton("Send message");

		exitButton = new ExitButton("Exit");

		sendButton.setFont(new Font("Arial", Font.BOLD, 16));

		chatWindow.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		// The output pane
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 6;
		c.insets = new Insets(10, 0, 10, 10);
		chatWindow.add(outputPane, c);

		// The friends pane
		c.gridx = 6;
		c.gridy = 0;
		c.gridwidth = 1;
		c.insets = new Insets(10, 0, 10, 0);
		chatWindow.add(friendsPane, c);

		// For the input pane
		c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 6;
		c.insets = new Insets(0, 0, 10, 0);
		c.anchor = GridBagConstraints.WEST;
		chatWindow.add(inputPane, c);

		// For the send button
		c.gridx = 0;
		c.gridy = 4;
		c.anchor = GridBagConstraints.LINE_START;
		c.insets = new Insets(0, 0, 10, 0);
		chatWindow.add(sendButton, c);

		// For the startChatbutton
		c.gridx = 6;
		c.gridy = 4; // 4
		c.anchor = GridBagConstraints.LINE_END;
		chatWindow.add(exitButton, c);

		// Adds chatWindow
		add(chatWindow, BorderLayout.CENTER);

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLocation(200, 200);
		setSize(800, 700);
		setVisible(true);

	}


	public void setOutput(String message) {
		String oldMessages = output.getText();
		oldMessages = oldMessages.substring(0, oldMessages.length() - 16);
		output.setText(oldMessages + message + "<br /></body>\n</html>");
	}

	public void sendMessage(String message) throws MalformedURLException {
		String temp = message + ":" + title;
		client.send("M " + temp);
	}

	KeyListener keyListener = new KeyListener() {
		public void keyPressed(KeyEvent keyEvent) {
			if (keyEvent.getKeyCode() == KeyEvent.VK_ENTER
					&& keyEvent.isControlDown()) {
				input.append("\n");
			} else if (keyEvent.getKeyCode() == KeyEvent.VK_ENTER) {
				sendButton.doClick();
				keyEvent.consume();
			}
		}

		public void keyReleased(KeyEvent keyEvent) {
			if (keyEvent.getKeyCode() == KeyEvent.KEY_RELEASED) {
				output.setText("Has written a text");
			}
		}

		public void keyTyped(KeyEvent keyEvent) {
			if (keyEvent.getKeyCode() == KeyEvent.KEY_PRESSED) {
				output.setText("Is writing a text");
			}
		}
	};

	private class SendButton extends JButton implements ActionListener {

		private static final long serialVersionUID = 27L;

		private SendButton(String name) {
			super(name);
			addActionListener(this);
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {

			if (input.getText() != "") {
				String message = input.getText();
				try {
					sendMessage(message);
				} catch (MalformedURLException e) {
					e.printStackTrace();
				}
			}
		}

	}

	private class ExitButton extends JButton implements ActionListener {

		private static final long serialVersionUID = 27L;

		private ExitButton(String name) {
			super(name);
			addActionListener(this);
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			client.send("L " + title);
			setVisible(false);
			sp.removeRoomFromLists(title);
			dispose();
		}
	}

	public void displayMessage(String message) {
		String tmp = output.getText();
		input.setText("");
		if (!tmp.equals("")) {
			output.setText(tmp + "\n" + message);
			output.setCaretPosition(output.getDocument().getLength());
		} else {
			output.setText(message);
			output.setCaretPosition(output.getDocument().getLength());
		}
	}

	public void mouseClicked(MouseEvent arg0) {
	}

	public void mouseEntered(MouseEvent arg0) {
	}

	public void mouseExited(MouseEvent arg0) {
	}

	public void mousePressed(MouseEvent arg0) {
	}

	public void mouseReleased(MouseEvent arg0) {
	}

	public void addName(String name) {
		if (!friendsModel.contains(name)) {
			friendsModel.addElement(name);
		}
	}

	public void removeName(String name) {
		friendsModel.removeElement(name);
	}

	public void reset() {
		friendsModel.clear();

	}
}
